Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		addList: [],
		isDefault: true

	},
	onLoad: function(options) {
		// console.log(options.addressStr + "------");
		//1 自提 2 外卖
		this.setData({
			type: options.type,
			state: options.state ? options.state : "", //add新增/edit编辑
			addressId: options.aId ? options.aId : "", //地址ID
			addressStr: options.addressStr ? options.addressStr : null //传过来的地址名字
		})
		if (options.state == "add") {
			getApp().globalData.common.reservebAdd = "";
			getApp().globalData.common.reservesAdd = "";
			this.setData({
				isDefaultData: 1 //新增时默认添加
			});
		}
	},
	onShow() {
		let _self = this;
		this.getShopInfo();

		//编辑时，通过ID获取用户一条地址
		if (_self.data.state == 'edit' && _self.data.addressId) {
			_self.getAddressInfo(_self.data.addressId);
		}
	},
	//根据ID获取一条地址
	getAddressInfo(aId) {
		let that = this;
		//请求去参数
		let parameterId = {
			url: getApp().globalData.baseUrl + '/applet/address/getAddressById.do',
			data: {
				addressId: aId
			}
		}
		//请求
		getApp().getRequest(parameterId).then((res) => {
			let addressAry = [];
			let addressObj = {};
			let isDefault = "";
			//console.log(that.data.addList);
			if (that.data.addList.length > 0) {
				that.data.addList.forEach(element => {
					if (element.name == res.data.obj.addressFixed) {
						addressObj.name = element.name; //固定地址
						addressObj.imgurl = 'meal_icon_selected'; //图标
						addressAry.push(addressObj);
					} else {
						element.imgurl = 'meal_icon_default';
						addressAry.push(element);
					}

				});
				that.setData({
					addList: addressAry,
				});
			}
			//是否默认
			if (res.data.obj.isDefault == 1) {
				isDefault = true;
			} else {
				isDefault = false;
			}
			that.setData({
				addressDetail: res.data.obj.addressDetail,
				isDefault: isDefault,
				isDefaultData: res.data.obj.isDefault
			});
			//固定地址/详细地址放入全局
			getApp().globalData.common.reservebAdd = res.data.obj.addressFixed;
			getApp().globalData.common.reservesAdd = res.data.obj.addressDetail;
		}).catch((err) => {
			console.log(err)
		});
	},
	chooseAdd: function(e) {
		for (var i = 0; i < this.data.addList.length; i++) {
			this.data.addList[i].imgurl = 'meal_icon_default';
		}
		this.data.addList[e.currentTarget.dataset.code].imgurl = 'meal_icon_selected';
		if (this.data.type == 1) {
			getApp().globalData.common.reserveSelfAdd = this.data.addList[e.currentTarget.dataset.code].name;

		} else {
			getApp().globalData.common.reservebAdd = this.data.addList[e.currentTarget.dataset.code].name;
		}

		//console.log(getApp().globalData.common.reserveSelfAdd);


		this.setData({
			addList: this.data.addList
		})

	},
	getShopInfo: function() {
		var _self = this;
		wx.showLoading({
			title: '加载中',
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/reserve/getShopSettings.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading(res.data.obj);
				if (res.data.code == 200) {
					console.log(res.data.obj.distributionAddress);
					var mjson = [];
					for (var i = 0; i < res.data.obj.distributionAddress.length; i++) {
						var json = {
							name: res.data.obj.distributionAddress[i],
							imgurl: 'meal_icon_default'
						}
						mjson.push(json)
					}
					if (_self.data.addressStr) {
						console.log("_self.data.addressStr")
						console.log(_self.data.addressStr)
						var mindex = -1;
						for (var i = 0; i < mjson.length; i++) {
							if (_self.data.addressStr == mjson[i].name) {
								mindex = i
							}
						}
						if (mindex == -1) {

						} else {
							mjson[mindex].imgurl = 'meal_icon_selected'
						}
					} else {

						if (getApp().globalData.common.reservebAdd) {
							var mindex = -1;
							for (var i = 0; i < mjson.length; i++) {
								if (getApp().globalData.common.reservebAdd == mjson[i].name) {
									mindex = i
								}
							}
							if (mindex == -1) {

							} else {
								mjson[mindex].imgurl = 'meal_icon_selected'
							}
						}

					}


					_self.setData({
						addList: mjson,
						address: getApp().globalData.common.reservesAdd
					})

				}
			}
		})
	},
	changeValue: function(e) {
		getApp().globalData.common.reservesAdd = e.detail.value;
	},
	turnAction: function(e) {
		if (this.data.type == 1) {
			wx.navigateBack({
				delta: 0
			})
			return;
		}
		let that = this;
		//console.log(getApp().globalData.common.reservebAdd);
		if (!getApp().globalData.common.reservebAdd) {
			wx.showToast({
				title: '请选择地址',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		if (!getApp().globalData.common.reservesAdd) {
			wx.showToast({
				title: '请填写详细地址',
				icon: 'none',
				duration: 2000
			})
			return;
		}
		//新增
		switch (this.data.state) {
			case "add":
				//添加 请求参数
				let parameterAdd = {
					url: getApp().globalData.baseUrl + '/applet/address/addDeliveryAddress.do?shopCode=' + getApp().globalData.common.reserveShopCode,
					method: "post",
					data: {
						addressFixed: getApp().globalData.common.reservebAdd,
						addressDetail: getApp().globalData.common.reservesAdd.replace(/<[^>]+>/g, ""),
						isDefault: that.data.isDefaultData
					}
				}
				console.log('that.data.isDefaultData' + that.data.isDefaultData)
				if (!that.data.isDefaultData) {
					getApp().globalData.common.reservebAdd = ""
					getApp().globalData.common.reservesAdd = ""
				}
				//添加 请求
				getApp().getRequest(parameterAdd).then((res) => {
					if (res.data.obj) {
						wx.navigateBack({
							delta: 1
						})
					}
				}).catch((err) => {
					console.log(err);
				});
				break;
				//编辑
			case "edit":
				//编辑 请求参数
				let parameterEdit = {
					url: getApp().globalData.baseUrl + '/applet/address/updateDeliveryAddress.do?shopCode=' + getApp().globalData.common.reserveShopCode,
					method: "post",
					data: {
						addressFixed: getApp().globalData.common.reservebAdd,
						addressDetail: getApp().globalData.common.reservesAdd,
						isDefault: that.data.isDefaultData,
						addressId: that.data.addressId
					}
				}
				// if (!that.data.isDefaultData) {
				// 	getApp().globalData.common.reservebAdd = ""
				// 	getApp().globalData.common.reservesAdd = ""
				// }
				//编辑地址 请求
				getApp().getRequest(parameterEdit).then((res) => {
					if (res.data.obj) {
						wx.navigateBack({
							delta: 1
						})
					}
				}).catch((err) => {
					console.log(err);
				});
				break;
		}

	},
	/*设置  默认地址*/
	switchChange(e) {
		//isDefault		1 默认，0 非默认
		//console.log(e.detail.value);
		if (e.detail.value) {
			this.setData({
				isDefaultData: 1
			});
		} else {
			this.setData({
				isDefaultData: 0
			});
		}
	}
})